<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
        placeholder="请输入学号"
        style="width: 200px"
        v-model="username"
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        style="margin: 10px"
        @click="findname"
        >搜索</el-button
      >
      <el-button
        type="info"
        icon="el-icon-remove-outline"
        style="margin: 10px"
        @click="reset"
        >重置</el-button
      >
    </div >
    <div style="margin: 10px 0; " >
      <el-button type="primary" icon="el-icon-search" @click="add"
        >新增</el-button
      >
      <el-button type="success"  icon="el-icon-circle-check" @click="exp"
        >批量导出</el-button
      >
      <el-upload  action="'http://'+serverIp+':8081/user/import'" :show-file-list="false" :on-change="importSuccess" accept="xlsx" style=" margin-left: 10px; display: inline-block;">
       <el-button type="warning" icon="el-icon-circle-close">批量导入</el-button>
      </el-upload>
    </div>
    <el-table :data="tableData">
      <el-table-column prop="id" label="id" width="140"> </el-table-column>
      <el-table-column prop="username" label="学生学号" width="120">
      </el-table-column>
      <el-table-column prop="role" label="权限"> </el-table-column>
       <el-table-column prop="className" label="班级名"> </el-table-column>
      <el-table-column prop="nickname" label="学生姓名"> </el-table-column>
      <el-table-column prop="course" label="课程"> </el-table-column>
      <el-table-column prop="scores" label="课程成绩"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[1,7,20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="成绩评定" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="50px">
        <el-form-item label="学号" >
          <el-input v-model="form.username" autocomplete="off" disabled></el-input>
        </el-form-item>
  
        <el-form-item label="姓名" >
          <el-input v-model="form.nickname"  autocomplete="off" disabled></el-input>
        </el-form-item>
     
        <el-form-item label="成绩">
          <el-input v-model="form.scores" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="个人评价">
          <el-input  type="textarea" :rows="8" v-model="form.selfEvaluation" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";
import { serverIp } from '@/utils/config';
export default {
  name: "user",

  data() {
    return {
      options:[],
      tableData: [],
      form: {},
      username: "",
      pageSize: 7,
      pageNum: 1,
      total: null,
      dialogFormVisible: false,
      name:JSON.parse(localStorage.getItem("user")).username
    };
  },
  methods: {
    load() {
      this.request
        .get("/user/page", {
          params: {
            username: this.username,
            pageNum: this.pageNum,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.load();
    },
    findname() {
      this.load();
    },
    add() {
      this.dialogFormVisible = true;
      this.form = {};
    },
    edit(row) {
      this.form = row;
      request.get("/role").then(res=>{
        this.options=res.data;
      })
      this.dialogFormVisible = true;
    },
    reset() {
      this.username = "";
    },
    exp() {
      window.open('http://'+serverIp+':8081/user/export');
      request.get('/user/export')
    },
    importSuccess(){
      this.$message.success("传入成功")
      this.load()
    },
    deleteById(id) {
      request.delete("/user/" + id).then((res) => {
        if (res) {
          this.$message.success("成功");
        } else {
          this.$message.error("失败");
        }
        this.dialogFormVisible = false;
        this.load();
      });
    },
    save() {
      this.request.post("/user/save", this.form,{params:{name:this.name}}).then((res) => {
        if (res) {
          this.$message.success("成功");
        } else {
          this.$message.error("失败");
        }
        this.dialogFormVisible = false;
        this.load();
      });
    },
  },
  created() {
    //请求分页数据
    this.load();
   
  },
};
</script>
<style scoped>
.up{
  margin-left: 10px;
}
.block{
    margin-top: 20px;
    display: flex;
    justify-content: center;
}
</style>